package ar.com.e2solver.view;

import java.awt.Button;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import ar.com.e2solver.exception.BusinessException;
import ar.com.e2solver.solver.SolverResult;

public class ActionButtonsView extends Panel {

	private static final long serialVersionUID = 1L;
	protected static String RUN_COMMAND = "RUN";
	protected static String RESET_COMMAND = "RESET";
	
	public ActionButtonsView(E2SolverView e2SolverView) {
		GridLayout gl = new GridLayout(1,2);
		this.setLayout(gl);
		
		ButtonActionListener listener = new ButtonActionListener(e2SolverView); 
		
		Button run = new Button("Correr");
		run.setActionCommand(RUN_COMMAND);
		run.addActionListener(listener);
		
		Button reset = new Button("Reiniciar");
		reset.setActionCommand(RESET_COMMAND);
		reset.addActionListener(listener);
		
		this.add(run);
		this.add(reset);
	}
	
	class ButtonActionListener implements ActionListener { 
		
		private E2SolverView mainView;
		public ButtonActionListener(E2SolverView e2SolverView) {
			this.mainView = e2SolverView;
		}

		public void actionPerformed(ActionEvent ae) {
			String command = ae.getActionCommand();
			if(RUN_COMMAND.equals(command)){
				this.mainView.getConfigurationView().updateConfiguration(); 
				SolverResult result = null;
				try {
					result = this.mainView.getE2Solver().run();
				} catch (BusinessException e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
				mainView.setResult(result);
			} else {
				this.mainView.clean();
			}
		}
	}
}


 